<?php
class Img{
	private $name;
	private $type;
	private $tmp_name;
	private $size;
	private $path = "/uploads";
	private $fileTypes = array('jpg','jpeg','gif','png','x-png');
	private $maxSize = '2048';//上传图片最大值，单位为KB

	private $filePath;
	private $thumbPath;
	private $fileName;
	private $tmpImg;
	private $thumbWidth = 80;
	private $thumbHeight = 60;
	public function __construct($imgInfo,$width=0,$height=0){
		if($width!=0&&$height!=0){
			$this->thumbWidth = $width;
			$this->thumbHeight = $height;
		}
		$this->name = $imgInfo['name'];
		$this->type = $imgInfo['type'];
		$this->tmp_name = $imgInfo['tmp_name'];
		$this->size = $imgInfo['size'];
		$this->filePath = $_SERVER['DOCUMENT_ROOT'].rtrim($this->path,'/').'/'.date('Ymd');
		$this->thumbPath = rtrim($this->filePath,'/').'/thumb';
		$this->fileName = time().rand(0,9).strrchr($this->name,'.');
	}
	public function saveUpload(){
		$this->miloMkdir($this->filePath);
		$targetFile = rtrim($this->filePath,'/').'/'.$this->fileName;
		$fileParts = pathinfo($this->name);
		if($this->size>($this->maxSize*1024*1024)){
			return "上传的文件不能超过".$this->maxSize."MB!";
		}
		if (in_array($fileParts['extension'],$this->fileTypes)) {
			move_uploaded_file($this->tmp_name,$targetFile);
			$this->createThumb($this->thumbWidth,$this->thumbHeight);
			return ltrim(substr($targetFile,strlen($_SERVER['DOCUMENT_ROOT'])),'/');
		} else {
			return 'Invalid file type.';
		}
	}
	private function createThumb($tWidth,$tHeight){
		$this->init_img();
		$this->miloMkdir($this->thumbPath);
		$width = imagesx($this->tmpImg);
		$height = imagesy($this->tmpImg);
		//$thumbWidth = $tWidth;
		//$thumbHeight = $tHeight;
		$ratio = $width/$height;
		$resize_ratio = $tWidth/$tHeight;
		$newimg = imagecreatetruecolor($tWidth,$tHeight);
		$white = @imagecolorallocate($newimg, 255, 255, 255);
		imagefill($newimg,0,0,$white);
		if($ratio == $resize_ratio){
			imagecopyresampled($newimg, $this->tmpImg, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height); 
			ImageJpeg ($newimg,rtrim($this->thumbPath,'/').'/'.$this->fileName);
		}elseif($ratio > $resize_ratio){
			$tmpWidth = $width/$tWidth*$tHeight;
			$xStart = ceil(($width-$tmpWidth)/2);
			imagecopyresampled($newimg, $this->tmpImg, 0, 0, $xStart, 0, $tWidth, $tHeight, $tmpWidth, $height); 
			ImageJpeg ($newimg,rtrim($this->thumbPath,'/').'/'.$this->fileName); 
		}else{
			$tmpHeight = $height/$tWidth*$tHeight;
			$yStart = ceil(($height-$tmpHeight)/2);
			imagecopyresampled($newimg, $this->tmpImg, 0, 0, 0, $yStart, $tWidth, $tHeight, $width, $tmpHeight); 
			ImageJpeg ($newimg,rtrim($this->thumbPath,'/').'/'.$this->fileName); 
		}
	} 
	private function init_img(){
		$targetFile = rtrim($this->filePath,'/').'/'.$this->fileName;
		$fileParts = pathinfo($this->name);
		$type = $fileParts['extension'];
		if($type=="jpg" || $type=='jpeg'){
			$this->tmpImg = imagecreatefromjpeg($targetFile); 
		}elseif($type=="gif"){ 
			$this->tmpImg = imagecreatefromgif($targetFile); 
		}elseif($type=="png" || $type=='x-png'){
			$this->tmpImg = imagecreatefrompng($targetFile); 
		}
	}
	private function miloMkdir($dir){
		return is_dir($dir) or ($this->miloMkdir(dirname($dir)) and mkdir($dir, 0777));
	}
}
?>
